using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.Utility;
using Microsoft.Practices.CompositeUI.EventBroker;

using LibrErian.Data;
using LibrErian.Model;
using LibrErian.Common;
using LibrErian.Services;
using LibrErian.Interfaces.Services;

namespace LibrErian.Modules.BasicBook
{
	public class SideBarViewPresenter : Presenter<ISideBarView>
	{
        private ILibraryService libraryService;
        private IBookService bookService;
        private IList libraries;

        [EventPublication("event://DisplayFilterChanged", PublicationScope.Global)]
        public event EventHandler<EventArgs<SearchArgs>> DisplayFilterChanged;

        [ServiceDependency]
        public IBookService BookService
        {
            set
            {
                bookService = value;
            }
        }

        [ServiceDependency]
        public ILibraryService LibraryService
        {
            set
            {
                libraryService = value;
            }
        }

        public void FilterByGenre(int genreId)
        {
            ApplyFilter(SearchType.Genre, genreId);
        }

        public void FilterByAuthor(int authorId)
        {
            ApplyFilter(SearchType.Author, authorId);
        }

        public void ClearFilter()
        {
            ApplyFilter(SearchType.All, -1);
        }

        public override void OnViewReady()
        {
            LoadData();
        }

        public void Refresh()
        {
            LoadBookData();
        }

        private void LoadData()
        {
            LoadBookData();
            LoadLibraryData();
        }

        private void LoadLibraryData()
        {
            libraries = libraryService.GetLibraries();

            View.InitializeLibraries(libraries);
        }

        private void LoadBookData()
        {
            IList genres = DataManager.GetGenres();
            IList authors = DataManager.GetAuthors();
            View.InitializeTreeview(genres, authors);
        }

        public void OnSelectdLibraryChanged(Library selectedLibrary)
        {
            bookService.SetCurrentLibrary(selectedLibrary);
            ClearFilter();
        }

        public void OnNewLibrary()
        {
            ControlledWorkItem<EditLibraryController> workItem = WorkItem.WorkItems.AddNew<ControlledWorkItem<EditLibraryController>>();
            Library newLibrary = workItem.Controller.Run(new Library());
            if (newLibrary != null)
            {
                libraries.Add(newLibrary);
                View.InitializeLibraries(libraries);
                View.SetSelectedLibrary(newLibrary);
            }
        }

        public void OnEditLibrary(Library library)
        {
            ControlledWorkItem<EditLibraryController> workItem = WorkItem.WorkItems.AddNew<ControlledWorkItem<EditLibraryController>>();
            Library modifiedLibrary = workItem.Controller.Run(library);
            if (modifiedLibrary != null)
            {
                LoadLibraryData();
                View.SetSelectedLibrary(modifiedLibrary);
            }
        }

        public void OnDeleteLibrary(Library library)
        {
            libraryService.Delete(library);
            LoadLibraryData();
        }

        private void ApplyFilter(SearchType searchType, int key)
        {
            if (DisplayFilterChanged != null)
            {
                SearchArgs args = new SearchArgs();
                args.SearchType = searchType;
                args.Key = key;
                DisplayFilterChanged(this, new EventArgs<SearchArgs>(args));
            }
        }
    }

    public interface ISideBarView
    {
        void InitializeLibraries(IList libraries);
        void InitializeTreeview(IList genres, IList authors);
        void SetSelectedLibrary(Library library);
    }
}
